<?php

namespace app\index\controller;

use app\common\ConfigBase;
use app\common\model\ToolModel;
use app\common\YujisoController;
use app\index\service\UserService;

class User extends YujisoController
{
    public function __construct()
    {
        parent::__construct();
    }
    //用户列表
    public function getList()
    {
        exitJson(UserService::getList($this->param));
    }
    //登陆
    public function login()
    {
        $rules = [
            'username' => 'require',
            'pass' => 'require',
        ];
        $this->validateData($rules);
        $ret = UserService::ssoLogin(trim($this->request->param('username')), $this->request->param('pass'));
        exitJson($ret);
    }
    //编辑及修改权限
    public function edit()
    {
        $param = $this->param;
        if ($this->request->isPost()) {
            $rules = [
                'uid' => 'require',
            ];
            $this->validateData($rules);
            UserService::edit($param);
            returnJson(1,'修改成功');
        } else {
            $permission=UserService::getPermission();
            $user=UserService::getEditUser($param);
            returnJson(1, '', ['permission' => $permission, 'user' => $user]);
        }
    }
    //启用 禁用
    public function disableOrEnable()
    {
        $param =$this->param;
        $rules = [
            'id' => 'require',
        ];
        $this->validateData($rules);
        ToolModel::updateData(['id' => $param['id']], ['status' => $param['status']],'user');
        returnJson(1,'成功');
    }
    //退出登陆
    public function logout(){
        session('user', null);
        returnJson();
    }
    //获取配置缓存到前端
    public function config(){
        if(UID){
            exitJson(UserService::config(UID));
        }
        returnJson(500);
    }
}
